import java.util.Arrays;
import java.util.Comparator;

// https://leetcode-cn.com/problems/non-overlapping-intervals/
// 435. 无重叠区间

class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, Comparator.comparingInt(a -> a[1]));

        int result = 1;
        int edge = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] >= edge) {
                result++;
                edge = intervals[i][1];
            }
        }

        return intervals.length - result;
    }
}